package com.yh.workbill.adapter;

import android.animation.ObjectAnimator;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.CompoundButton;

import com.yh.workbill.R;
import com.yh.workbill.databinding.ItemCountinfoBinding;
import com.yh.workbill.model.WorkBill;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

public class CountInfoAdapter extends RecyclerView.Adapter<CountInfoAdapter.ViewHolder> {

    private List<WorkBill> list;

    private OnCheckedLister onCheckedLister;

    public CountInfoAdapter(List<WorkBill> list) {
        this.list = list;
    }

    public void setList(List<WorkBill> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void setOnCheckedLister(OnCheckedLister onCheckedLister) {
        this.onCheckedLister = onCheckedLister;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemCountinfoBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_countinfo, parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        WorkBill workBill = list.get(position);
        holder.binding.setData(workBill);
        boolean check = workBill.isCheck();
        holder.binding.cbSelect.setChecked(check);

        holder.itemView.setOnClickListener(v -> {
            if (holder.binding.llShow.getVisibility() == View.VISIBLE) {
                holder.binding.llShow.setVisibility(View.GONE);
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(holder.binding.tvDown, "rotation", 179f, 0f);
                objectAnimator.setDuration(300);
                objectAnimator.setInterpolator(new LinearInterpolator());
                objectAnimator.start();
            } else {
                holder.binding.llShow.setVisibility(View.VISIBLE);
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(holder.binding.tvDown, "rotation", 0f, 179f);
                objectAnimator.setDuration(300);
                objectAnimator.setInterpolator(new LinearInterpolator());
                objectAnimator.start();
            }
        });

        holder.binding.cbSelect.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (onCheckedLister != null) {
                onCheckedLister.onCheckedChanged(isChecked, workBill.getUuid());
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ItemCountinfoBinding binding;

        public ViewHolder(@NonNull ItemCountinfoBinding itemView) {
            super(itemView.getRoot());
            this.binding = itemView;
        }
    }

    public interface OnCheckedLister {
        void onCheckedChanged(boolean isChecked, String  uuid);
    }
}
